package com.wbcs.config.component.application.report;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.wbcs.config.component.application.report.extendconfig.AbsExtendConfigBean;
import com.wbcs.config.component.container.AbsContainerConfigBean;
import com.wbcs.config.component.container.page.PageBean;
import com.wbcs.exception.WbcsConfigLoadingException;

public abstract class AbsConfigBean implements Cloneable
{
    private Map<String,AbsExtendConfigBean> mExtendConfigForReportType=new HashMap<String,AbsExtendConfigBean>();

    private AbsConfigBean parent;

    public AbsConfigBean(AbsConfigBean parent)
    {
        this.parent=parent;
    }

    public void setMExtendConfigForReportType(Map<String,AbsExtendConfigBean> extendConfigForReportType)
    {
        mExtendConfigForReportType=extendConfigForReportType;
    }

    public void setExtendConfigDataForReportType(String key,AbsExtendConfigBean extendConfigDataForReportType)
    {
        mExtendConfigForReportType.put(key,extendConfigDataForReportType);
    }

    public AbsExtendConfigBean getExtendConfigDataForReportType(String key)
    {
        return mExtendConfigForReportType.get(key);
    }

    public AbsExtendConfigBean getExtendConfigDataForReportType(Class extendConfigBeanClass)
    {
        if(mExtendConfigForReportType==null||extendConfigBeanClass==null) return null;
        for(Entry<String,AbsExtendConfigBean> entry:mExtendConfigForReportType.entrySet())
        {
            if(extendConfigBeanClass.isInstance(entry.getValue())) return entry.getValue();
        }
        return null;
    }

    public AbsConfigBean getParent()
    {
        return parent;
    }

    public void setParent(AbsConfigBean parent)
    {
        this.parent=parent;
    }

    public ReportBean getReportBean()
    {
        if(this instanceof ReportBean)
        {
            return (ReportBean)this;
        }
        AbsConfigBean bean=this.getParent();
        if(bean==null) return null;
        while(!(bean instanceof ReportBean))
        {
            bean=bean.getParent();
            if(bean==null) return null;
        }
        return (ReportBean)bean;
    }

    public AbsContainerConfigBean getParentContainer()
    {
        return getReportBean().getParentContainer();
    }

    public PageBean getPageBean()
    {
        return getParentContainer().getPageBean();
    }

    public AbsConfigBean clone(AbsConfigBean parent)
    {
        try
        {
            AbsConfigBean newbean=(AbsConfigBean)super.clone();
            newbean.setParent(parent);
            return newbean;
        }catch(CloneNotSupportedException e)
        {
            throw new WbcsConfigLoadingException("加载报表配置文件失败",e);
        }
    }

    protected void cloneExtendConfig(AbsConfigBean newConfigBean)
    {
        if(mExtendConfigForReportType!=null)
        {
            Map<String,AbsExtendConfigBean> mTemp=new HashMap<String,AbsExtendConfigBean>();
            newConfigBean.setMExtendConfigForReportType(mTemp);
            for(Entry<String,AbsExtendConfigBean> entryTmp:mExtendConfigForReportType.entrySet())
            {
                if(entryTmp.getValue()==null) continue;
                mTemp.put(entryTmp.getKey(),entryTmp.getValue().clone(newConfigBean));
            }
        }
    }
}
